package one.bugu.android.demon.util;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.EditText;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RuleUtils {
    private static final String MATCH_PHONE_REGEX = "((13)|(14)|(15)|(17)|(18))\\d{9}$";
    private static final String MATCH_QQ_REGEX = "^[1-9][0-9]{4,}$";
    private static final String SMS_SIX_CODER = "\\d{6}";
    private static final String USER_CARD_REGEX = "\\d{17}[0-9a-zA-Z]";
    private static final String MATCH_ALL_CHARACTERS = "[a-zA-Z]*";
    private static final String MATCH_NUMBER_REGEX = "\\d+";
    private static final String USER_CARD_REGEX_HONGKONG = "[A-Za-z][0-9]{6}\\([0-9Aa]\\)";
    private static final String USER_CARD_REGEX_AOMEN = "[157][0-9]{6}\\([0-9A-Za-z]\\)";
    private static final String USER_CARD_REGEX_TAIWAN = "^[A-Z][0-9]{9}$";
    private static final String USER_CARD_REGEX_ALL = "^[A-Za-z0-9()]{6,18}$";

    private RuleUtils() {
    }

    public static void checkQQRegex(String QQ) {
        if (!Pattern.matches(MATCH_QQ_REGEX, QQ)) {
            throw new IllegalArgumentException("请输入正确的QQ号");
        }
    }

    public static void checkPhoneRegex(String phonNum) {
        if (!Pattern.matches(MATCH_PHONE_REGEX, phonNum)) {
            throw new IllegalArgumentException("请输入正确的手机号");
        }
    }

    public static void checkUserCardRegex(String userCard) {
        if (!(Pattern.matches(USER_CARD_REGEX_ALL, userCard))) {
            throw new IllegalArgumentException("请输入正确的身份证号");
        }
    }

    public static void checkUserCardRegexOld(String userCard) {
        if (!(Pattern.matches(USER_CARD_REGEX, userCard) || Pattern.matches(USER_CARD_REGEX_HONGKONG, userCard) || Pattern.matches(USER_CARD_REGEX_AOMEN, userCard) || Pattern.matches(USER_CARD_REGEX_TAIWAN, userCard))) {
            throw new IllegalArgumentException("请输入正确的身份证号");
        }
    }

    public static void checkEmpty(EditText et, String errMsg) {
        if (TextUtils.isEmpty(et.getText().toString())) {
            throw new IllegalArgumentException(errMsg);
        }
    }

    public static void checkEmpty(String content, String errMsg) {
        if (TextUtils.isEmpty(content)) {
            throw new IllegalArgumentException(errMsg);
        }
    }


    public static void checkEmojiCharacter(EditText et) {
        String input = et.getText().toString();
        for (int pos = 0; pos < input.length(); pos++) {
            char codePoint = input.charAt(pos);
            boolean isEmoji = !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
            if (isEmoji) {
                throw new IllegalArgumentException("含有非法的表情符号");
            }
        }
    }

    public static void checkSMSCaptchaLength(String code) {
        if (code.length() < 6) {
            throw new IllegalArgumentException("短信验证码长度为6位");
        }
    }


    public static boolean patternPhoneRegex(String phonNum) {
        return Pattern.matches(MATCH_PHONE_REGEX, phonNum);
    }

    public static String patternCode(String patternContent) {
        if (TextUtils.isEmpty(patternContent)) {
            return null;
        }
        Pattern p = Pattern.compile(SMS_SIX_CODER);
        Matcher matcher = p.matcher(patternContent);
        if (matcher.find()) {
            return matcher.group();
        }
        throw new IllegalStateException("没有匹配6位验证码");
    }

    public static boolean drawableEquals(Drawable a, Drawable b) {
        return a.getConstantState().equals(b.getConstantState());
    }

    public static void checkIsNotAllNumber(String number) {
        if (Pattern.matches(MATCH_NUMBER_REGEX, number)) {
            throw new IllegalArgumentException("请输入数字");
        }
    }

    public static void checkAllNumber(String number) {
        if (Pattern.matches(MATCH_NUMBER_REGEX, number)) {
            throw new IllegalArgumentException("不能全是数字");
        }
    }

    public static String findNumber(String characters) {
        Matcher matcher = Pattern.compile(MATCH_NUMBER_REGEX).matcher(characters);
        matcher.find();
        return matcher.group();
    }

    public static void checkAllCharacters(String pwd) {
        if (Pattern.matches(MATCH_ALL_CHARACTERS, pwd)) {
            throw new IllegalArgumentException("密码过于简单,不能是纯英文的密码");
        }
    }

    public static void checkAllSame(String pwd) {
        String[] result = pwd.split("" + pwd.charAt(0));
        if (result.length == 0) {
            throw new IllegalArgumentException("密码过于简单,字符不能完全相同");
        }
    }

    public static void checkBankNoIsEqual(String bankNo, String againBankNo) {
        if (!TextUtils.equals(bankNo, againBankNo)) {
            throw new IllegalArgumentException("两次输入的账号不相同，请核对后再次输入");
        }
    }

    public static void checkPwdIsEqual(String pwd, String newpwdAgain) {
        if (!TextUtils.equals(newpwdAgain, pwd)) {
            throw new IllegalArgumentException("两次输入的密码不相同，请核对后再次输入");
        }
    }

    public static void checkPwdLength(String pass) {
        if (pass.length() < 6 || pass.length() > 20) {
            throw new IllegalArgumentException("密码位数一般为6~20位非中文字符");
        }
    }

    public static void checkUnSelectTip(String content, String tip) {
        if (TextUtils.equals(content, tip)) {
            throw new IllegalArgumentException(tip);
        }
    }
}
